/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-5-2   22:29
* filename: LaborRenderSystem.cpp
-----------------------------------------------------------------------------
*/
#include "../include/Renderer/LaborRenderSystem.h"
#include "../include/Renderer/LaborRenderState.h"
#include "../include/Renderer/LaborConstantBuffer.h"
NAMESPACE_LABOR3D_BEGIN
//---------------------------------------------------------------------
CLRenderSystem::CLRenderSystem()
{
	m_f2DZValue = 1000.0f;
}

//---------------------------------------------------------------------
CLRenderSystem::~CLRenderSystem()
{

}

//-----------------------------------------------------------------------------
// Frustum data
float fFrustumLeft = 0.f, fFrustumRight = 0.f;
float fFrustumTop = 0.f,  fFrustumBottom = 0.f;
float fFrustumNearPlane = 0.f, fFrustumFarPlane = 0.f;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void CLRenderSystem::makeProjectionMatrix( float left, float right, float bottom, float top, float nearPlane, float farPlane, CLMatrix4x4 &matrixProjection )
{
	fFrustumLeft = left;
	fFrustumRight = right;
	fFrustumBottom = bottom;
	fFrustumTop = top;
	fFrustumNearPlane = nearPlane;
	fFrustumFarPlane = farPlane;

	CLMatrix4x4 projection;

	CLHPoint row;
	row.x = 2.0f*nearPlane / (right-left);
	row.y = 0.0f;
	row.z = 0.0f;
	row.w = 0.0f;
	projection.setRow( 0, row );

	row.x = 0.0f;
	row.y = 2.0f * nearPlane / (top-bottom);
	row.z = 0.0f;
	row.w = 0.0f;
	projection.setRow( 1, row );

	row.x = (left+right) / (right-left);
	row.y = (top+bottom) / (top-bottom);
	row.z = farPlane / (nearPlane-farPlane);
	row.w = -1.0f;
	projection.setRow( 2, row );

	row.x = 0.0f;
	row.y = 0.0f;
	row.z = nearPlane * farPlane / (nearPlane-farPlane);
	row.w = 0.0f;
	projection.setRow( 3, row );

	projection.transpose();

	CLMatrix4x4 rotMat(CLPoint( (PI / 2.0f), 0.0f, 0.0f));

	projection *= rotMat;

	//	setProjectionMatrix( projection );
	matrixProjection = projection;
}

//-----------------------------------------------------------------------------
// Sets frustum by FOV in degrees along the horizontal axis
void CLRenderSystem::makeProjectionMatrix( float FOVx, float aspectRatio, float nearPlane, float farPlane, CLMatrix4x4 &matrixProjection )
{
	/*
	h = cos(fov/2) / sin(fov/2);
	w = h / aspect;

	2*zn/w  0       0              0
	0       2*zn/h  0              0
	0       0       zf/(zf-zn)     1
	0       0       zn*zf/(zn-zf)  0
	*/
	float h = 1.0f / tanf( FOVx*0.5f );
	float w = h / aspectRatio;

	CLMatrix4x4 mat;
	CLHPoint    column;

	// fill in the projection matrix
	column.set( w, 0.0f, 0.0f, 0.0f );
	mat.setRow( 0, column );

	column.set( 0.0f, h, 0.0, 0.0f );
	mat.setRow( 1, column );

	column.set( 0.0, 0.0, farPlane/(farPlane-nearPlane), 1.0f );
	mat.setRow( 2, column );

	column.set( 0.0, 0.0, nearPlane*farPlane/(nearPlane-farPlane), 0.0f );
	mat.setRow( 3, column );


	matrixProjection = mat;
}

NAMESPACE_LABOR3D_END